<template>
  <el-dialog
    title="添加策略"
    top="50px"
    :visible.sync="addStrategy"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    :before-close="closeModel"
    append-to-body>
    <hr class="boundary">
    <currency-strategy-info  @getNewData="getNewData" ref="strategyInfo"></currency-strategy-info>
    <hr class="boundary">
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeModel">取 消</el-button>
      <el-button type="primary" @click="addSucc">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import currencyStrategyInfo from './public/currencyStrategyInfo'


  export default {
    props: {
      deviceTypeId: Number,
      addStrategy: Boolean
    },
    components: {
      currencyStrategyInfo
    },
    data () {
      return {
        showAdd: false,
        currencyStrategyInfo: {}
      }
    },
    methods: {
      getNewData (newData) {
        this.currencyStrategyInfo = newData
      },

      addSucc () {

        let self = this
        $.ajax({
          type: 'POST',
          url: this.api + 'currencyStrategy',
          headers: {
            'Content-Type': 'application/json',
            'X-Requested-With': 'XMLHttpRequest'
          },
          xhrFields: {
            withCredentials: true
          },
          data: JSON.stringify({
            'name': this.currencyStrategyInfo.strategyName,
            'userId': 317102060456,
            'week': this.currencyStrategyInfo.weekNum,
            'startTime': this.currencyStrategyInfo.startTimeHour + ':' + this.currencyStrategyInfo.startTimeMin + ':00',
            'endTime': this.currencyStrategyInfo.endTimeHour + ':' + this.currencyStrategyInfo.endTimeMin + ':00',
            'minPeople':this.currencyStrategyInfo.minPeople,
            'maxPeople':this.currencyStrategyInfo.maxPeople,
            'deviceTypeId': this.deviceTypeId,
            'switch':this.currencyStrategyInfo.switch
          }),
          success (data) {
            console.log(data)
            if (data > 0) {
              self.closeModel()
              self.$message({
                message: '策略添加成功！',
                type: 'success'
              })
            } else {
              self.$message.error('添加失败请重试！')
            }
          }
        })
      },

      closeModel () {
        this.showAdd = false
        this.$refs.strategyInfo.clearData()
        this.$emit('closeModel')
      },
    },
    watch: {
      addStrategy (newVal) {
        this.showAdd = newVal
      }
    },
  }
</script>

<style scoped>
  @import "../../../assets/public/subModal.css";

  .el-dialog__wrapper >>> .el-dialog {
    width: 500px;
  }

  .el-button {
    margin: 0 0 0 10px;
    border-radius: 8px;
    font-size: 15px;
    padding: 10px 20px;
  }
</style>
